﻿using System;
using System.Web.Mvc;
using Fuse8.ContentManagementFramework.Domain;
using Fuse8.DomainFramework.Common.Locator;

namespace Fuse8.ContentManagementFramework.Web.MvcPowered.Components
{
    public static class CmsSitePropertiesHtmlExtensions
    {
        /// <summary>
        /// Renders site meta field by specified name.
        /// </summary>
        /// <typeparam name="TProperty">Meta Field Type</typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="propertyName">Meta Field name</param>        
        /// <remarks>If Template is not specified it renders result of method ToString() of TProperty object</remarks>
        /// <returns></returns>
        public static CmsSitePropertyComponent<TProperty> SiteProperty<TProperty>(this HtmlHelper htmlHelper, string propertyName, string format = "")
        {
            return new CmsSitePropertyComponent<TProperty>(htmlHelper, propertyName, format);
        }
    }

    public class CmsSitePropertyComponent<TProperty> : CmsMvcBaseComponent<CmsSitePropertyComponent<TProperty>, TProperty>
    {
        #region Fields

        private CmsSite _currentSite = DependencyServiceLocator.Current.GetInstance<ICmsContext>().ContextSite;
        private Func<TProperty, bool> _checkFunction;

        #endregion

        #region Property

        public int MaxLevelUp { get; set; }

        public bool Inhereted { get; set; }

        public string Property { get; set; }

        public string FormatString { get; set; }

        protected override TProperty Value
        {
            get { return _currentSite.GetMetaValue<TProperty>(Property); }
        }

        #endregion

        #region Constructor

        public CmsSitePropertyComponent(HtmlHelper htmlHelper, string property, string formatString)
            : base(htmlHelper)
        {
            Property = property;

            FormatString = formatString;

            Component = this;
        }

        #endregion

        #region Methods

        public CmsSitePropertyComponent<TProperty> HideIf(Func<TProperty, bool> check)
        {
            _checkFunction = check;
            return this;
        }

        protected override string RenderComponent()
        {
            string result = null;

            if (_checkFunction != null && _checkFunction(Value))
            {
                return "";
            }

            if (InlineTemplate != null)
            {
                result = base.RenderComponent();
            }
            else if (!string.IsNullOrWhiteSpace(Property))
            {
                var workingValue = Value;
                if (workingValue != null)
                {
                    result = ConvertToString(workingValue);

                    if (!string.IsNullOrWhiteSpace(result) && !string.IsNullOrWhiteSpace(FormatString))
                    {
                        result = string.Format(FormatString, result);
                    }
                }
            }

            return result ?? string.Empty;
        }

        protected virtual string ConvertToString(TProperty workingValue)
        {
            return workingValue.ToString();
        }               

        #endregion
    }
}